package com.sgb.kjwl.view.ui.activity.lookingforAJob;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;

import androidx.databinding.DataBindingUtil;

import android.graphics.drawable.Drawable;

import android.text.TextUtils;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityFindWorksBinding;
import com.sgb.kjwl.di.lookingforAJob.DaggerWorksComponent;
import com.sgb.kjwl.di.lookingforAJob.WorksModule;
import com.sgb.kjwl.model.entity.BiddingEntity;
import com.sgb.link.entity.PickFilterEntity;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.packers.PickResultEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.adapter.pick.PickBidingAdapter;
import com.sgb.kjwl.view.widget.PickPopWindow1;
import com.sgb.kjwl.view.widget.ThreeLevelAreaWindow;
import com.sgb.kjwl.view.widget.ThreeLevelFilterWindow;
import com.sgb.link.view.widget.XLinearLayoutManager;
import com.sgb.kjwl.viewmodel.lookingforAJob.FindWorksAtvModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.recyclerview.entity.BaseAreaEntity;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

/**
 * 劳务分包
 */
public class FindWorksActivity extends AppActivity {

    @Inject
    public
    FindWorksAtvModel viewModel;
    private ActivityFindWorksBinding binding;
    //private FindWorksAdapter findWorksAdapter;
    private PickBidingAdapter pickingAdapter;

    //private FindWorksFilterPopWindow popWindow;

    //工程专项
    private ThreeLevelFilterWindow popCategory;
    private PickResultEntity pickResultProject1;
    private PickResultEntity pickResultProject2;
    private PickResultEntity pickResultProject3;
    private String projectNo = "";
    //概算金额
    private PickPopWindow1 popPrice;
    private PickResultEntity pickResultPrice;
    //发布时间
    private PickPopWindow1 popTime;
    private PickResultEntity pickResultTime;
    //所在区域
    private ThreeLevelAreaWindow popCity;
    private PickResultEntity pickResultProvince;
    private PickResultEntity pickResultCity;
    private PickResultEntity pickResultArea;
    private boolean isClick;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerWorksComponent.builder().appComponent(appComponent).worksModule(new WorksModule(this)).build().inject(this);
    }

    @Override
    protected boolean statusBarDark() {
        return false;
    }

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, FindWorksActivity.class);
        context.startActivity(intent);
    }

    private void initFilterData(boolean isLoadMore) {
        viewModel.getBiddingList("2", "", pickResultPrice.getNo(), "", pickResultProvince.getNo(), pickResultCity.getNo(), projectNo, "1", isLoadMore);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_find_works);
        binding.setViewModel(viewModel);
        binding.include.ivBack.setOnClickListener(this);
        binding.include.rightView.setOnClickListener(this);
        binding.tvPrice.setOnClickListener(this);
        binding.tvProject.setOnClickListener(this);
        binding.tvTime.setOnClickListener(this);
        binding.tvArea.setOnClickListener(this);
        binding.include.titleView.setText("劳务分包");

        if (pickingAdapter == null) {
            pickingAdapter = new PickBidingAdapter(getActivity(), null);
        }

        pickingAdapter.setOnItemClick(new AdapterOnItemClick<BiddingEntity>() {
            @Override
            public void onItemClick(BiddingEntity tenderListEntity, int position) {
                viewModel.getUrlSetting(tenderListEntity.getId());
            }
        });
        pickingAdapter.onIChildClick(tenderListEntity -> viewModel.getUrlSetting(tenderListEntity.getId()));

        binding.recyclerView.setLayoutManager(new XLinearLayoutManager(this));
        binding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(
                0,
                0,
                0,
                getResources().getDimension(R.dimen.dp5)));
        binding.recyclerView.setAdapter(pickingAdapter);
        binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initFilterData(false);
            }

            @Override
            public void onLoadMore() {

                initFilterData(true);

            }
        });

        pickResultProject1 = new PickResultEntity();
        pickResultProject2 = new PickResultEntity();
        pickResultProject3 = new PickResultEntity();
        pickResultPrice = new PickResultEntity();
        pickResultTime = new PickResultEntity();
        pickResultProvince = new PickResultEntity();
        pickResultCity = new PickResultEntity();
        pickResultArea = new PickResultEntity();
    }

    @Override
    protected void init() {
        pickResultProvince.setNo(viewModel.getAreaCode());
        setTextViewChange(binding.tvArea, R.mipmap.ic_down_arrow, R.color.color_main);
        binding.tvArea.setText(viewModel.getAreaName());
        initFilterData(false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_project:
                if (popCategory == null) {
                    viewModel.getTenderGoodsCategoryAllList("2");
                } else {
                    showCategoryPop(null);
                }
                break;
            case R.id.tv_price:
                if (popPrice == null) {
                    viewModel.getPriceData();
                } else {
                    showPricePopWindow(null);
                }
                break;
            case R.id.tv_time:
                if (popTime == null) {
                    viewModel.getPickTimes();
                } else {
                    showTimePopWindow(null);
                }
                break;
            case R.id.tv_area:
                if (popCity == null) {
                    viewModel.getCityList();
                } else {
                    showCityPop(null);
                }
                break;
        }
    }

    public void showList(List<BiddingEntity> list, boolean isLoadMore, boolean isLast) {
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list != null && list.size() > 0) {
                pickingAdapter.addDatas(list);
            }
            binding.recyclerView.setNoMore(isLast);
        } else {
            binding.recyclerView.refreshComplete();
            if (list != null && list.size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                pickingAdapter.setDatas(list);
                binding.recyclerView.smoothScrollToPosition(0);
            } else {
                pickingAdapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    //工程-工程分类
    public void showCategoryPop(List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>> data) {
        if (popCategory == null) {
            popCategory = new ThreeLevelFilterWindow(getActivity());
            popCategory.setBgView(binding.grayLayout);
            popCategory.setOnResultClick(new ThreeLevelFilterWindow.OnResultClick() {
                @Override
                public void result(Object key1, Object key2, Object key3) {
                    pickResultProject1.setNo(((PickResultEntity) key1).getNo());
                    pickResultProject1.setName(((PickResultEntity) key1).getName());
                    pickResultProject2.setNo(((PickResultEntity) key2).getNo());
                    pickResultProject2.setName(((PickResultEntity) key2).getName());
                    pickResultProject3.setNo(((PickResultEntity) key3).getNo());
                    pickResultProject3.setName(((PickResultEntity) key3).getName());
                    if (TextUtils.equals("全部", pickResultProject1.getName())) {
                        projectNo = "";
                        binding.tvProject.setText("工程分类");
                    } else {
                        if (TextUtils.equals("全部", pickResultProject3.getName())) {
                            if (TextUtils.equals("全部", pickResultProject2.getName())) {
                                projectNo = pickResultProject1.getNo();
                                binding.tvProject.setText(pickResultProject1.getName());
                            } else {
                                projectNo = pickResultProject2.getNo();
                                binding.tvProject.setText(pickResultProject2.getName());
                            }
                        } else {
                            projectNo = pickResultProject3.getNo();
                            binding.tvProject.setText(pickResultProject3.getName());
                        }
                    }
                    initFilterData(false);
                }

                @Override
                public void reset() {
                    pickResultProject1.clear();
                    pickResultProject2.clear();
                    pickResultProject3.clear();
                    projectNo = "";
                    binding.tvProject.setText("工程分类");
                    initFilterData(false);
                }
            });
            popCategory.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    if (TextUtils.isEmpty(pickResultProject1.getNo())) {
                        setTextViewChange(binding.tvProject, R.mipmap.ic_down_arrow_n, R.color.color_666666);
                    } else {
                        setTextViewChange(binding.tvProject, R.mipmap.ic_down_arrow, R.color.color_main);
                    }
                }
            });
            popCategory.setThreeLevelData1(data);
        }
        setTextViewChange(binding.tvProject, R.mipmap.icon_up_red, R.color.color_main);
        popCategory.showPopwindow0(binding.clLayout);
    }

    //概算金额pop
    public void showPricePopWindow(List<PickSelectEntity> data) {
        if (popPrice == null) {
            popPrice = new PickPopWindow1(this);
            popPrice.setBgView(binding.grayLayout);
            popPrice.setOnResultClick(new PickPopWindow1.OnResultClick() {
                @Override
                public void result(Object key1, Object key2, Object key3) {
                    pickResultPrice.setNo((String) key1);
                    pickResultPrice.setName((String) key2);
                    pickResultPrice.setPosition((int) key3);
                    if (TextUtils.isEmpty((String) key2) || TextUtils.equals("全部", (String) key2)) {
                        binding.tvPrice.setText("概算金额");
                    } else {
                        binding.tvPrice.setText((String) key2);
                    }
                    initFilterData(false);
                }
            });
            popPrice.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    if (TextUtils.isEmpty(pickResultPrice.getName()) || TextUtils.equals("全部", pickResultPrice.getName())) {
                        setTextViewChange(binding.tvPrice, R.mipmap.ic_down_arrow_n, R.color.color_666666);
                    } else {
                        setTextViewChange(binding.tvPrice, R.mipmap.ic_down_arrow, R.color.color_main);
                    }
                }
            });

            popPrice.setData(data);
        }
        setTextViewChange(binding.tvPrice, R.mipmap.icon_up_red, R.color.color_main);
        popPrice.showPopwindow0(binding.clLayout);
    }

    //发布时间pop
    public void showTimePopWindow(List<PickSelectEntity> data) {
        if (popTime == null) {
            popTime = new PickPopWindow1(this);
            popTime.setBgView(binding.grayLayout);
            popTime.setOnResultClick(new PickPopWindow1.OnResultClick() {
                @Override
                public void result(Object key1, Object key2, Object key3) {
                    pickResultTime.setNo((String) key1);
                    pickResultTime.setName((String) key2);
                    pickResultTime.setPosition((int) key3);
                    if (TextUtils.isEmpty((String) key2) || TextUtils.equals("全部", (String) key2)) {
                        binding.tvTime.setText("发布时间");
                    } else {
                        binding.tvTime.setText((String) key2);
                    }
                    initFilterData(false);
                }
            });
            popTime.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    if (TextUtils.isEmpty(pickResultTime.getName()) || TextUtils.equals("全部", pickResultTime.getName())) {
                        setTextViewChange(binding.tvTime, R.mipmap.ic_down_arrow_n, R.color.color_666666);
                    } else {
                        setTextViewChange(binding.tvTime, R.mipmap.ic_down_arrow, R.color.color_main);
                    }
                }
            });

            popTime.setData(data);
        }
        setTextViewChange(binding.tvTime, R.mipmap.icon_up_red, R.color.color_main);
        popTime.showPopwindow0(binding.clLayout);
    }

    //省市区pop
    public void showCityPop(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data) {
        if (popCity == null) {
            popCity = new ThreeLevelAreaWindow(getActivity()){
                @Override
                protected void provinceClick(BaseAreaEntity baseAreaEntity, int position) {
                    super.provinceClick(baseAreaEntity, position);
                    pickResultProvince.setNo(baseAreaEntity.getNo());
                    pickResultProvince.setName(baseAreaEntity.getSimpleName());
                    if ("全部".equals(baseAreaEntity.getSimpleName()) || pickResultProvince.getName().equals(baseAreaEntity.getSimpleName())){
                        pickResultCity.setNo("");
                        pickResultCity.setName("");
                    }
                }

                @Override
                protected void cityClick(BaseAreaEntity baseAreaEntity, int position) {
                    super.cityClick(baseAreaEntity, position);
                    isClick = true;
                    pickResultCity.setNo(baseAreaEntity.getNo());
                    pickResultCity.setName(baseAreaEntity.getSimpleName());
                    if ("全部".equals(baseAreaEntity.getSimpleName()) || pickResultCity.getName().equals(baseAreaEntity.getSimpleName())){
                        pickResultArea.setNo("");
                        pickResultArea.setName("");
                    }
                }

                @Override
                protected void areaClick(BaseAreaEntity baseAreaEntity, int position) {
                    super.areaClick(baseAreaEntity, position);
                    pickResultArea.setNo(baseAreaEntity.getNo());
                    pickResultArea.setName(baseAreaEntity.getSimpleName());
                }
            };
            popCity.setBgView(binding.grayLayout);
            popCity.setShowSimpleName(true);
            popCity.setProvinceSingle();
            popCity.setCitySingle();
            popCity.setAreaSingle();
            pickResultProvince.setName(viewModel.getAreaName());
            popCity.setOnResultClick(new ThreeLevelAreaWindow.OnResultClick() {
                @Override
                public void result(Object key1, Object key2, Object key3) {
                    initFilterData(false);
                    if (!"全部".equals(pickResultArea.getName())&&!"".equals(pickResultArea.getName())){
                        binding.tvArea.setText(pickResultArea.getName());
                        return;
                    }else {
                        binding.tvArea.setText(pickResultCity.getName());
                    }

                    if (!"全部".equals(pickResultCity.getName())&&!"".equals(pickResultCity.getName())){
                        binding.tvArea.setText(pickResultCity.getName());
                        return;
                    }else {
                        binding.tvArea.setText(pickResultProvince.getName());
                    }

                    if (!"全部".equals(pickResultProvince.getName())&&!"".equals(pickResultProvince.getName())){
                        binding.tvArea.setText(pickResultProvince.getName());
                    }else {
                        if (!isClick){
                            binding.tvArea.setText(viewModel.getAreaName());
                        }else {
                            binding.tvArea.setText("地区");
                        }
                    }
                }
            });
            popCity.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    if (TextUtils.isEmpty(pickResultProvince.getNo())) {
                        setTextViewChange(binding.tvArea, R.mipmap.ic_down_arrow_n, R.color.color_666666);
                    } else {
                        setTextViewChange(binding.tvArea, R.mipmap.ic_down_arrow, R.color.color_main);
                    }
                }
            });

            popCity.setDataForAll(data, true, viewModel.getAreaCode());
        }
        setTextViewChange(binding.tvArea, R.mipmap.icon_up_red, R.color.color_main);
        popCity.showPopwindow(binding.clLayout, 0, 0);
    }

    private void setTextViewChange(TextView textView, int img, int textColor) {
        Resources res = getResources();
        Drawable imgs = res.getDrawable(img);
        imgs.setBounds(0, 0, imgs.getMinimumWidth(), imgs.getMinimumHeight());
        textView.setCompoundDrawables(null, null, imgs, null);
        textView.setTextColor(getResources().getColor(textColor));
    }

    public void getUrl(String url, String id) {
        WebViewX5HostActivity.start(FindWorksActivity.this, url, id);
    }
}
